package com.tansun.easycare.rule.datamodel.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.DataSetTreeVo;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.datamodel.dao.RuleEnumDao;
import com.tansun.easycare.rule.datamodel.entity.RuleEnum;
import com.tansun.easycare.rule.datamodel.service.RuleEnumService;

/**
 * 枚举项管理ServiceImpl
 * @author hejiatai
 * @version 2017-12-08
 */
@Service("ruleEnumService")
@Transactional(readOnly = true)
public class RuleEnumServiceImpl extends RuleBaseServiceImpl<RuleEnumDao,RuleEnum> implements RuleEnumService{
    @Autowired
    private RuleEnumDao ruleEnumDao;

	public RuleEnum get(String id) {
		
		return super.get(id);
	}
	
	public List<RuleEnum> findList(RuleEnum ruleEnum) {
		return super.findList(ruleEnum);
	}
	
	
	public PageInfo<RuleEnum> findPage(Page<RuleEnum> page, RuleEnum ruleEnum) {
		return super.findPage(page, ruleEnum);
	}
	
	@Transactional(readOnly = false)
	public void save(RuleEnum ruleEnum) {
		super.save(ruleEnum);
	}
	
	@Transactional(readOnly = false)
	public void delete(RuleEnum ruleEnum) {
		super.delete(ruleEnum);
	}

	@Override
	public List<DataSetTreeVo> findTypeListByProjCode(String projectCode) {		
        List<DataSetTreeVo> resultList = new ArrayList<DataSetTreeVo>();
        RuleEnum ruleEnum = new RuleEnum();
        RuleEnum ruleEnum2 = null;
        ruleEnum.setSearchEnumType(true);
        List<RuleEnum> list = this.ruleEnumDao.findList(ruleEnum);
       
        for (RuleEnum re : list) {
            DataSetTreeVo enumSet = new DataSetTreeVo();
            enumSet.setName(re.getEnumName());
            enumSet.setNameEN(re.getEnumCode());
            ruleEnum2= new RuleEnum();
            ruleEnum2.setEnumType(re.getEnumCode());
            List<DataSetTreeVo> children = new ArrayList<DataSetTreeVo>();
            List<RuleEnum> list2 = this.ruleEnumDao.findTypeList(ruleEnum2);
            if (list2 == null || list2.size() < 1) {
                continue;
            }
            for (RuleEnum re2 : list2) {
                DataSetTreeVo enumSet2 = new DataSetTreeVo();
                enumSet2.setId(re2.getEnumType());
                enumSet2.setName(re2.getEnumName());
                enumSet2.setNameEN(re2.getEnumCode());
                children.add(enumSet2);
            }
            enumSet.setChildren(children);
            resultList.add(enumSet);
        }
        return resultList;
	}

	@Override
	public void insertList(List<RuleEnum> ruleenum) {
		for (RuleEnum type : ruleenum) {
			this.save(type);
		}
	}

	
}